import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_module/page_androidview.dart';
import 'package:flutter_module/page_b.dart';
import 'package:flutter_module/page_keyevent.dart';
import 'package:flutter_module/page_listview.dart';
import 'package:flutter_module/page_valuekey.dart';
import 'package:flutter_module/util/AbsState.dart';
import 'package:flutter_module/util/transition.dart';

import 'util/log.dart';

/// 默认入口，空白即可
class DefaultHomePage extends StatefulWidget {
  static const String routeName = "/";
  const DefaultHomePage();

  @override
  State<DefaultHomePage> createState() => _DefaultHomePageState();

  @override
  MyElement createElement() => MyElement(this);
}

class MyElement extends StatefulElement {
  MyElement(StatefulWidget widget) : super(widget);
  @override
  void mount(Element? parent, dynamic newSlot) {
    super.mount(parent, newSlot);
    logger.print('$widget 触发mount生命周期');
  }
  @override
  void deactivate() {
    super.deactivate();
    logger.print('$widget 触发deactivate生命周期');
  }
  @override
  void unmount() {
    super.unmount();
    logger.print('$widget 触发unmount生命周期');
  }
  @override
  Widget build() {
    logger.print('$widget 触发build生命周期');
    return super.build();
  }

}

class _DefaultHomePageState extends AbsState<DefaultHomePage> {

  @override
  Widget build(BuildContext context) {
    logger.print("$widget build");
    return Scaffold(
      appBar: AppBar(
        // 因为是路由栈的最底层，所以不会自动处理返回，我们要自己处理
        leading: GestureDetector(
          child: Icon(Icons.arrow_back),
          // 退出 FlutterActivity，iOS 不支持，要单独处理
          onTap: () => SystemNavigator.pop(),
        ),
        title: Text('Default Home Page'),
      ),
      body:Column(
        children: [
          Container(
              child: ElevatedButton(
                onPressed: () {
                  go(context, widget,const PageB());
                  // Navigator.push(context, MaterialPageRoute(builder: (context) => const PageB()));
                  // Navigator.of(context).pushNamed(PageB.routeName, arguments: "a home message");
                },
                child: Text('goto page_b'),
              )),
          Container(
              child: ElevatedButton(
                onPressed: () {
                  go(context, widget,const PageValuekey(PageValuekey.routeName));
                  // Navigator.push(context, MaterialPageRoute(builder: (context) => const PageB()));
                  // Navigator.of(context).pushNamed(PageB.routeName, arguments: "a home message");
                },
                child: Text('goto PageValuekey'),
              )),
          Container(
              child: ElevatedButton(
                onPressed: () {
                  go(context, widget,ListViewPage());
                  // Navigator.push(context, MaterialPageRoute(builder: (context) => const PageB()));
                  // Navigator.of(context).pushNamed(PageB.routeName, arguments: "a home message");
                },
                child: Text('goto ListViewPage'),
              )),
          Container(
              child: ElevatedButton(
                onPressed: () {
                  go(context, widget,PageAndroidview());
                  // Navigator.push(context, MaterialPageRoute(builder: (context) => const PageB()));
                  // Navigator.of(context).pushNamed(PageB.routeName, arguments: "a home message");
                },
                child: Text('goto PageAndroidview'),
              )),
          Container(
              child: ElevatedButton(
                onPressed: () {
                  go(context, widget,KeyEventPage());
                  // Navigator.push(context, MaterialPageRoute(builder: (context) => const PageB()));
                  // Navigator.of(context).pushNamed(PageB.routeName, arguments: "a home message");
                },
                child: Text('goto KeyEventPage'),
              )),
        ]
      )
    );
  }
}
